package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.bean.AreaInfo;
import com.lidroid.xutils.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 2.0版本中的界面
 * 区域
 */
public class AreaActivity extends BaseActivity {

    private ListView lv_area;
    private List<AreaInfo.Area> value;
    private EditText et_project;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_area);
        ImageView iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        lv_area = (ListView) findViewById(R.id.lv_area);
        String uuid = SPUtil.getString(this, "uuid");
        final String level = SPUtil.getString(this, "level");
        String userId = getIntent().getStringExtra("userId");
        initData(uuid, userId);
        et_project = (EditText) findViewById(R.id.et_project);
        et_project.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    et_project.clearFocus(); //失去焦点
                    Intent intent = new Intent();
                    intent.setClass(AreaActivity.this, MainActivity.class);
                    intent.putExtra("level", level);
                    startActivity(intent);
                    overridePendingTransition(R.anim.myfadein, R.anim.myfadeout);
                }
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        et_project.clearFocus(); //失去焦点
    }
    //区域

    private void initData(String uuid, String userId) {
        RequestParams params = new RequestParams();
        params.addBodyParameter("userId", userId);
        Utils.requestData("加载中 . . .", this, "/construction/queryTerritoryList/" + uuid, params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {


                try {
                    JSONObject object = new JSONObject(json);
                    String status = object.getString("status");
                    String msg = object.getString("msg");
                    if ("1".equals(status)) {
                        processData(json);

                    } else if ("0".equals(status)) {
                        Toast.makeText(AreaActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(String json) {

            }
        });


    }

    private void processData(String json) {
        Gson gson = new Gson();
        AreaInfo areaInfo = gson.fromJson(json, AreaInfo.class);
        value = areaInfo.value;
        if (areaInfo.value != null) {

            lv_area.setAdapter(new myAdapter());
            lv_area.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                    Intent intent = new Intent();
                    intent.putExtra("areaId", value.get(i).id);
                    intent.putExtra("name", value.get(i).name);
                    intent.setClass(AreaActivity.this, MainActivity.class);
                    startActivity(intent);
                }
            });

        }
    }

    private class myAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return value.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v = LayoutInflater.from(AreaActivity.this).inflate(R.layout.lv_area_item, null);
            TextView tv_area_name = (TextView) v.findViewById(R.id.tv_area_name);
            TextView tv_number = (TextView) v.findViewById(R.id.tv_number);
            tv_area_name.setText(value.get(i).name);
            tv_number.setText(value.get(i).isonlineCount + "/" + value.get(i).count);

            return v;
        }
    }

}
